package com.bondex.charter.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.bondex.charter.config.ExcelHandler;
import com.bondex.charter.pojo.excelvo.EntrustVO;
import com.bondex.charter.pojo.excelvo.ISFVO;
import com.bondex.charter.pojo.excelvo.LoadVO;
import com.bondex.charter.service.ExcelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * <pre>
 * 描述：TODO
 * </pre>
 *
 * @author： wh
 * @date: 2021/9/16 13:43
 */
@RestController
@RequestMapping("/excel")
public class ExcelController extends BaseController {

    @Autowired
    private ExcelService excelService;


    @Operation(summary = "excel导出－ISF")
    @RequestMapping(value="/exportISF", method = RequestMethod.GET)
    public void exportISF(@Parameter(description = "token") String token,HttpServletResponse response) throws IOException {
        //查询需要导出的数据
        List<ISFVO> isfvoList = excelService.getISFList();
        ServletOutputStream out = response.getOutputStream();
        //通知浏览器以附件的形式下载处理，设置返回头要注意文件名有中文
        response.setHeader("Content-disposition", "attachment;filename=" +URLEncoder.encode("ISF-BAY PLAN", "UTF-8") + ".xlsx");
        response.setContentType("multipart/form-data");
        response.setCharacterEncoding("utf-8");
        //输出流到浏览器下载
        EasyExcel.write(out).needHead(true)
                .head(ISFVO.class)
                .excelType(ExcelTypeEnum.XLSX)
                .registerWriteHandler(new ExcelHandler())
                .registerWriteHandler(ExcelHandler.getStyleStrategy())
                .sheet(0, "sheet1")
                .doWrite(isfvoList);
    }

    @Operation(summary = "excel导出－配载明细")
    @RequestMapping(value="/exportLoad", method = RequestMethod.GET)
    public void exportLoad(@Parameter(description = "token") String token,HttpServletResponse response) throws IOException {
        //查询需要导出的数据
        List<LoadVO> loadvoList = excelService.getLoadList(token);
        ServletOutputStream out = response.getOutputStream();
        //通知浏览器以附件的形式下载处理，设置返回头要注意文件名有中文
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("配载明细", "UTF-8") + ".xlsx");
        response.setContentType("multipart/form-data");
        response.setCharacterEncoding("utf-8");
        //输出流到浏览器下载
        EasyExcel.write(out).needHead(true)
                .head(LoadVO.class)
                .excelType(ExcelTypeEnum.XLSX)
                .registerWriteHandler(new ExcelHandler())
                .registerWriteHandler(ExcelHandler.getStyleStrategy())
                .sheet(0, "sheet1")
                .doWrite(loadvoList);
    }

    @Operation(summary = "excel导出－委托明细")
    @RequestMapping(value="/exportEntrust", method = RequestMethod.GET)
    public void exportEntrust(@Parameter(description = "token") String token,HttpServletResponse response, @RequestParam(required = true, value = "list") List<String> list) throws IOException {
        List<EntrustVO> entrustVOList = excelService.getEntrustList(token,list);
        ServletOutputStream out = response.getOutputStream();
        //通知浏览器以附件的形式下载处理，设置返回头要注意文件名有中文
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("委托明细", "UTF-8") + ".xlsx");
        response.setContentType("multipart/form-data");
        response.setCharacterEncoding("utf-8");
        //输出流到浏览器下载
        EasyExcel.write(out).needHead(true)
                .head(EntrustVO.class)
                .excelType(ExcelTypeEnum.XLSX)
                .registerWriteHandler(new ExcelHandler())
                .registerWriteHandler(ExcelHandler.getStyleStrategy())
                .sheet(0, "sheet1")
                .doWrite(entrustVOList);
    }

}
